package com.liliya.servlet.admin;

import com.liliya.pojo.Role;
import com.liliya.pojo.User;
import com.liliya.service.RoleService;
import com.liliya.service.UserService;
import com.liliya.service.impl.RoleServiceImpl;
import com.liliya.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2021/1/6 15:09
 */
@WebServlet(urlPatterns = "/admin/add")
public class UserAddServlet extends HttpServlet {
    private RoleService roleService = new RoleServiceImpl();
    private UserService userService = new UserServiceImpl();

    //添加一个用户
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String roleNum = req.getParameter("role");
        System.out.println("用户名是："+username);
        System.out.println("密码是："+password);
        System.out.println("权限是："+roleNum);
        //先查权限
        Role role = roleService.findRole(Integer.parseInt(roleNum));
        System.out.println(role);
        User user = new User(username,password,role);
        userService.addUser(user);
        //然后就是跳转到操作成功的页面
        resp.sendRedirect(req.getContextPath()+"/success.jsp");
    }
}
